package com.example.echoewish.ui.my_activity.my_comment_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.MyComment;
import com.example.echoewish.repository.CommentMessageRepository;
import com.example.echoewish.util.LogUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyCommentViewModel extends ViewModel {
    public static final int DELETE_MY_COMMENT_SUCCESS=3;
    public static final int DELETE_MY_COMMENT_FAIL=4;

    MutableLiveData<List<MyComment>>myCommentMutableLiveData;
    MutableLiveData<MyComment> deleteCommentMutableLiveData;
    MutableLiveData<String> responseMutableLiveData;


    public MyCommentViewModel()
    {
        myCommentMutableLiveData=new MutableLiveData<>();
        deleteCommentMutableLiveData=new MutableLiveData<>();
        responseMutableLiveData=new MutableLiveData<>();
    }

    public void getCommentByUserId()
    {
        CommentMessageRepository.getInstance().queryMyCommentByUserId(UserInfo.getInstance().UserId,new GetMyCommentCallBack());
    }

    public void deleteComment()
    {
        CommentMessageRepository.getInstance().deleteComment(deleteCommentMutableLiveData.getValue(),new DeleteCommentCallBack());
    }

    private class GetMyCommentCallBack implements Callback<List<MyComment>>
    {

        @Override
        public void onResponse(Call<List<MyComment>> call, Response<List<MyComment>> response) {
            myCommentMutableLiveData.postValue(response.body());
        }

        @Override
        public void onFailure(Call<List<MyComment>> call, Throwable t) {
            new LogUtil().w(LogUtil.CALLBACK_TAG,"删除我的评论信息 回调失败"+t);
            myCommentMutableLiveData.postValue(null);
        }
    }

    private class DeleteCommentCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                responseMutableLiveData.postValue(response.body().string());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            new LogUtil().w(LogUtil.CALLBACK_TAG,"删除我的评论信息 回调失败"+t);
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
        }
    }
}
